package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.pojo.entity.Address;
import com.example.service.AddressService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 收货地址 controller 层
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @PostMapping("/add")
    public Result add(@RequestBody Address address) {
        this.addressService.add(address);
        return Result.success();
    }

    @GetMapping("/pageQuery")
    public Result pageQuery(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Address address
    ) {
        PageResult<Address> pageResult = this.addressService.pageQuery(pageNum, pageSize, address);
        return Result.success(pageResult);
    }

    @GetMapping("/query")
    public Result queryById(@RequestParam Integer id) {
        Address address = this.addressService.queryById(id);
        return Result.success(address);
    }

    @GetMapping("/query/all")
    public Result queryAll(Address address) {
        List<Address> addressList = this.addressService.queryAll(address);
        return Result.success(addressList);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Address address) {
        this.addressService.update(address);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        this.addressService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        this.addressService.deleteBatch(ids);
        return Result.success();
    }
}
